package com.zs4sdk.activity.login;

import android.app.Activity;
import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.widget.Button;

import com.common.library.data.CommonHandle;
import com.common.library.entity.JSONResultEntity;
import com.common.library.util.Helper;
//import com.zs4gamesdk.R;
import com.zs4gamesdk.R;
import com.zs4sdk.common.Extra;
import com.zs4sdk.entity.LoginJSONResultEntity;
import com.zs4sdk.statistics.Zs4GameStatistics;

import org.json.JSONObject;

/**
 * Created by Administrator on 2018/4/17.
 */

  class Zs4GameLoginProxy extends Zs4GameLoginProxyBase implements View.OnClickListener {
    protected View imgZs4GameBack;
    private Button btnZs4GameGotoGame;
    private Button btnOtherAccountLogin;
    protected Zs4GameLoginBase mZs4GameTouristLogin;
    public Zs4GameLoginProxy(Activity act,View v){
        super(act,v);
        imgZs4GameBack = mMainView.findViewById(R.id.img_zs4_game_back);

        btnOtherAccountLogin = (Button)mMainView.findViewById(R.id.btn_zs4_game_other_account_login);
        btnZs4GameGotoGame = (Button)mMainView.findViewById(R.id.btn_zs4_game_goto_game);
        //  stuPhoneLogin.setVisibility(View.VISIBLE);
        imgZs4GameBack.setOnClickListener(this);
        mZs4GameTouristLogin = new Zs4GameTouristLogin(mActivity,mMainView,myHandle);
        mZs4GameTouristLogin.setOnClickListener(this);
        mZs4GameTouristLogin.showLogin();
      //  mZs4GameOtherAccountLogin.showLogin();
        init();
    }

    protected void init(){
        mZs4GameOtherAccountLogin = new Zs4GameOtherAccountLogin(mActivity,mMainView,myHandle);
        mZs4GameLoginInputPhone = new Zs4GameLoginInputPhone(mActivity,mMainView,myHandle);
        mZs4GameLoginInputPhone.setOnClickListener(this);
        mZs4GameOtherAccountLogin.setOnClickListener(this);
    }

    protected void doOtherAccountLogin(){
        imgZs4GameBack.setVisibility(View.VISIBLE);
        mZs4GameTouristLogin.hideLogin();
        //  mZs4GameLoginOtherAccount.showLogin();
        //  loginStyle = OTHER_ACCOUNT;
        mZs4GameLoginInputPhone.showLogin();
        loginStyle = INPUT_PHONE;
    }
    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btn_zs4_game_other_account_login ){
            doOtherAccountLogin();
        }else if(id == R.id.btn_zs4_game_goto_game){
            mZs4GameTouristLogin.login();
        }else if(id == R.id.btn_zs4game_next){
            if(mZs4GameLoginInputPhone.isCanDoNext()){
                mZs4GameLoginInputPhone.login();
            }
        }else if(id == R.id.img_zs4_game_back){
            onBackPressed();
        }else if(id == R.id.zs4gameviewid_btn_login_with_verifycode){
            mZs4GameOtherAccountLogin.login();
        }
    }

    CommonHandle myHandle = new CommonHandle(mActivity, new CommonHandle.HandleMessage() {

        @Override
        public void doHandleMessage(Message msg) {
            if(Extra.MessageKey.REQUESTCOMPLETE_LASTLOGINACCOUNT == msg.what){
                Bundle bundle = msg.getData();
                mZs4GameTouristLogin.handleResult(bundle);
            }else if (Extra.MessageKey.REQUEST_SUCCESS == msg.what) {
                String type = "";
                String account = "";
                String password = "";
                Bundle bundle = msg.getData();
                if (Helper.isNotNull(bundle.containsKey("str"))) {
                    if (bundle.containsKey("param1")) {
                        type = bundle.getString("param1");
                    }
                    if (bundle.containsKey("param2")) {
                        account = bundle.getString("param2");
                    }
                    if (bundle.containsKey("param3")) {
                        password = bundle.getString("param3");
                    }
                }
                JSONObject jsonObject1 = (JSONObject) msg.obj;
                Zs4GameLoginBase.loginSuccess(mActivity,type,jsonObject1,account,password);
                String userId = jsonObject1.optString(LoginJSONResultEntity.COLUMN_USERID);
                Zs4GameStatistics.setLoginSuccessBusiness(userId);
            }

            //手动获取验证码
            else if (Extra.MessageKey.REQUEST_VERIFY_SUCCESS == msg.what) {
                JSONResultEntity entity = new JSONResultEntity((JSONObject) msg.obj);
                //Log.e("phone","JSONResultEntity is  "+entity.toString());
                if (Helper.isNotNull(entity) && !entity.isSuccess()) {
                    mZs4GameOtherAccountLogin.setError(entity.getDesc());
                }
            }
            // 切换账号
            else if (Extra.MessageKey.COMFIRM_EXCHANGEACCOUNT == msg.what) {
                // exchangeAccount((LoginJSONResultEntity) msg.obj);
            }
            // 删除账号
            else if (Extra.MessageKey.COMFIRM_DELETEACCOUNT == msg.what) {
                //  deleteAccount((LoginJSONResultEntity) msg.obj);
            }
            //判断手机号码状态 注册过还是没有注册过,设置过密码还是没有设置密码
            else if (Extra.MessageKey.VERIFY_PHONE_REGIDTER_OR_LOGIN == msg.what) {
                gotoPhoneLogin(msg.arg1);
            }
        }
    });


    public void onDestroy(){

    }

    public void onBackPressed(){
        switch (loginStyle){
            case TOURIST_LOGIN:
                break;
            case INPUT_PHONE:
                loginStyle = TOURIST_LOGIN;
                imgZs4GameBack.setVisibility(View.GONE);
                mZs4GameTouristLogin.showLogin();
                mZs4GameLoginInputPhone.hideLogin();
                break;
            case OTHER_ACCOUNT:
                loginStyle = TOURIST_LOGIN;
                imgZs4GameBack.setVisibility(View.GONE);
                mZs4GameTouristLogin.showLogin();
                mZs4GameLoginInputPhone.hideLogin();
                break;
            case PHONE_LOGIN:
                mZs4GameLoginInputPhone.showLogin();
                mZs4GameOtherAccountLogin.hideLogin();
                loginStyle = INPUT_PHONE;
                break;
        }
    }
}
